package com.nbjtjc.safe.service.safe;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.safe.SafeInspectTableMapper;
import com.nbjtjc.safe.model.safe.SafeInspectTable;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SafeInspectTableServiceImpl {

    @Autowired
    private SafeInspectTableMapper tableMapper;

    public Result search(SafeInspectTable table){
        Result result=new Result();
        PageHelper.startPage(table.getNum(),table.getPageSize());
        List<SafeInspectTable> tableList= tableMapper.search(table);
        result.setData(new PageInfo<>(tableList));
        return result;
    }

    public Result add(SafeInspectTable table){
        Result result=new Result();
        table.setCreateUserId(MyShiroUtils.getSessionUserId());
        table.setCreateTime(new Date());
        tableMapper.insert(table);
        //todo 添加tableList的插入
        result.setData(table);
        return result;
    }
    public Result delete(SafeInspectTable table){
        Result result=new Result();
        if(ObjectUtil.isNotNull(table.getId())){
            tableMapper.deleteByPrimaryKey(table.getId());
        }
        result.setData(table);
        return result;
    }

    public Result update(SafeInspectTable table){
        Result result=new Result();
        tableMapper.updateByPrimaryKeySelective(table);
        result.setData(table);
        return result;
    }
}
